Skip to content

Reduce code complexity by extracting helpers and eliminating duplication#4

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-complex-code
Draft

Reduce code complexity by extracting helpers and eliminating duplication#4
Copilot wants to merge 4 commits intomainfrom
copilot/refactor-complex-code

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 21, 2025

Several functions exceeded cyclomatic complexity thresholds (CC 12-20), and ~100 lines of error handling were duplicated between sync and async clients.

Changes

Shared Error Handler

  • Extracted error_handler.py with handle_error_response() function
  • Both DexClient and AsyncDexClient delegate to shared implementation
  • Eliminates 100+ lines of duplication

Deduplication Module Refactoring

merge_cluster (CC 16→3)

  • _select_primary_row(): Primary contact selection logic
  • _merge_contact_fields(): Field merging with null-coalescing
  • _consolidate_related_records(): Email/phone consolidation and deduplication

find_fuzzy_name_duplicates (CC 12→3)

  • _create_soundex_blocks(): Soundex-based blocking for O(n²)→O(n) reduction
  • _find_matches_in_block(): Jaro-Winkler similarity matching within blocks

Script Refactoring

review_duplicates.py:main (CC 20→10)

  • _fetch_unresolved_groups(): Database query isolation
  • _display_contact_group(): Rich table rendering
  • _handle_user_choice(): User input processing

sync_with_integrity.py:save_contacts_batch (CC 12→8)

  • _check_contact_changed(): Hash comparison and metadata retrieval
  • _enrich_contact_data(): Name parsing and job title extraction
  • _save_contact_related_data(): Email/phone persistence

Example

Before:

# client.py and async_client.py both had identical 51-line _handle_error method
def _handle_error(self, response, endpoint):
    status_code = response.status_code
    # ... 51 lines of error handling logic

After:

# Shared in error_handler.py
def handle_error_response(response, endpoint):
    # ... single implementation

# Both clients delegate
def _handle_error(self, response, endpoint):
    handle_error_response(response, endpoint)

Testing

  • Added 26 tests covering all extracted helpers
  • All 186 tests pass (160 existing + 26 new)
  • Type checking and linting verified
Original prompt

Identify and suggest improvements to overly complex code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 21, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 3 commits December 21, 2025 14:08
…plication

Co-authored-by: domfahey <789732+domfahey@users.noreply.github.com>
Co-authored-by: domfahey <789732+domfahey@users.noreply.github.com>
Co-authored-by: domfahey <789732+domfahey@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify improvements for overly complex code Reduce code complexity by extracting helpers and eliminating duplication Dec 21, 2025
Copilot AI requested a review from domfahey December 21, 2025 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants